package com.lecture.java8.multithreading.pool;

import com.lecture.java8.multithreading.callable.CallableTest2;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author luyn
 * @version 1.0
 * @description 使用线程池
 *
 * @date 2021/1/15 15:04
 */
public class PoolTest {
    public static void main(String[] args) {
        // 提供指定数量的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        // 指定线程操作
        executorService.execute(new NumberThread());
        Future<Integer> submit = executorService.submit(new CallableTest2());
        try {
            System.out.println("总和：" + submit.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
    }
}

class NumberThread implements Runnable {
    @Override
    public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + ":" +i);
            }
    }
}
